package com.tarena.lbs.passport.service;

import com.tarena.lbs.base.protocol.exception.BusinessException;

import com.tarena.lbs.common.passport.converter.LoginConverter;
import com.tarena.lbs.common.passport.encoder.JwtEncoder;
import com.tarena.lbs.common.passport.enums.PassportResultEnum;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.passport.utils.MD5Util;

import com.tarena.lbs.passport.repository.LoginRepository;
import com.tarena.lbs.pojo.passport.bo.AdminBO;
import com.tarena.lbs.pojo.passport.bo.UserBO;
import com.tarena.lbs.pojo.passport.param.AdminLoginParam;
import com.tarena.lbs.pojo.passport.param.UserLoginParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginService {
    @Autowired
    private LoginRepository loginRepository;
    @Autowired
    private LoginConverter loginConverter;
    @Autowired
    private JwtEncoder jwtEncoder;

    public AdminBO doLogin(AdminLoginParam param) throws BusinessException {
        String phone = param.getPhone();
        AdminBO adminBO = loginRepository.getUserByPhone(phone);
        if (adminBO == null) {
            throw new BusinessException(PassportResultEnum.ADMIN_NONE_EXIST);
        }
        matchMD5Password(param.getPassword(), adminBO.getAccountPassword());
        generateJwt(adminBO);
        return adminBO;
    }

    private void generateJwt(AdminBO adminBO) throws BusinessException {
        UserPrinciple userPrinciple = loginConverter.adminbo2principal(adminBO);
        if (userPrinciple == null) {
            throw new BusinessException(PassportResultEnum.ADMIN_NONE_EXIST);
        }
        String jwt = jwtEncoder.generateToken(userPrinciple);
        adminBO.setJwt(jwt);
    }

    private void matchMD5Password(String password, String accountPassword) throws BusinessException {
        if (StringUtils.isEmpty(password)) {
            throw new BusinessException(PassportResultEnum.EMPTY_PASSWORD);
        }
        if (StringUtils.isEmpty(accountPassword)) {
            throw new BusinessException(PassportResultEnum.EMPTY_ACCOUNT_PASSWORD);
        }
        String md5Password = MD5Util.getMd5(password);
        if (!StringUtils.equals(md5Password, accountPassword)) {
            throw new BusinessException(PassportResultEnum.ADMIN_PASSWORD_ERROR);
        }
    }

    public UserBO doLogin(UserLoginParam param) throws BusinessException {
        //1. 根据用户名查询用户信息
        String username = param.getPhone();
        UserBO userBO = loginRepository.getUserByUsername(username);
        //2. 判断用户是否存在
        if (userBO == null) {
            throw new BusinessException(PassportResultEnum.USER_NONE_EXIST);
        }
        //3. 判断密码是否正确
        matchPassword(param.getPassword(), userBO.getPassword());
        //4. 生成jwt
        generateJwt(userBO);
        return userBO;
    }

    private void generateJwt(UserBO bo) throws BusinessException {
        UserPrinciple userPrinciple = loginConverter.userbo2principal(bo);
        if (userPrinciple == null) {
            throw new BusinessException(PassportResultEnum.USER_NONE_EXIST);
        }
        String jwt = jwtEncoder.generateToken(userPrinciple);
        bo.setJwt(jwt);
    }

    private void matchPassword(String inputPw, String dbPw) throws BusinessException {
        //非空
        if (StringUtils.isEmpty(inputPw) || StringUtils.isEmpty(dbPw)) {
            throw new BusinessException(PassportResultEnum.USER_PASSWORD_ERROR);
        }
        //非等
        if (!inputPw.equals(dbPw)) {
            throw new BusinessException(PassportResultEnum.USER_PASSWORD_ERROR);
        }
    }
}
